package com.platform.user.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.result.ApiException;
import com.platform.common.result.ApiExceptionEnum;
import com.platform.user.dao.UserDao;
import com.platform.user.model.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author GH
 */
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public boolean isExist(String account) {
        return userDao.lambdaQuery()
                .eq(UserDO::getAccount, account)
                .oneOpt().isPresent();
    }

    public UserDO getByAccount(String account) {
        UserDO userDO = userDao.lambdaQuery()
                .eq(UserDO::getAccount, account)
                .one();
        if (userDO == null) {
            throw new ApiException(ApiExceptionEnum.USER_NOT_FOUND);
        }
        return userDO;
    }

    public UserDO getById(String id) {
        UserDO userDO = userDao.lambdaQuery()
                .eq(UserDO::getId, id)
                .one();
        if (userDO == null) {
            throw new ApiException(ApiExceptionEnum.USER_NOT_FOUND);
        }
        return userDO;
    }

    public Boolean updateById(UserDO userDO) {
        return userDao.updateById(userDO);
    }

    public Boolean save(UserDO userDO) {
        return userDao.save(userDO);
    }

    public Boolean removeById(String id) {
        return userDao.removeById(id);
    }

    public String idToAccount(String id) {
        return getById(id).getAccount();
    }

    public String idToNickname(String id) {
        return getById(id).getNickname();
    }

    public Page<UserDO> page(Integer page, Integer size) {
        return userDao.page(new Page<>(page, size));
    }


    public UserDO getPasswordById(String id) {
        return userDao.lambdaQuery().select(UserDO::getId, UserDO::getPassword).eq(UserDO::getId, id).one();
    }

    public UserDO getNicknameById(String id) {
        return userDao.lambdaQuery().select(UserDO::getId, UserDO::getPassword).eq(UserDO::getId, id).one();
    }
}
